package com.framework.core.aspect;

import com.framework.core.exception.DemoModeException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 演示环境AOP
 *
 * @author Kevin
 */
@Aspect
@Component
@Slf4j
public class DemoModelAspect {

    @Value("${app.demoEnabled}")
    private boolean demoEnabled;

    @Pointcut("execution(public * com.framework..*.controller..*(..))")
    public void pointcut() {
    }

    @Before("pointcut()")
    public void before() {
        HttpServletRequest requset = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (demoEnabled
                && "POST".equals(requset.getMethod())
                && !requset.getRequestURI().contains("/login")
                && !requset.getRequestURI().contains("/logout")) {
            throw new DemoModeException();
        }
    }

}

